package com.sxjh.controller.train;

import com.sxjh.common.BaseController;
import com.sxjh.common.Result;
import com.sxjh.entity.TrainBookedEntity;
import com.sxjh.entity.param.TrainParam;
import com.sxjh.service.TrainBookedService;
import com.sxjh.uitl.POIUtil;
import com.sxjh.uitl.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/train")
@Api(tags = "火车管理")
public class TrainController extends BaseController {

    @Autowired
    private TrainBookedService trainBookedService;

    @ApiOperation("火车出入账报表")
    @GetMapping("/trainBooked")
    Result<PageUtil<TrainBookedEntity>> getTrainBooked(TrainParam param){
        return Result.ok(trainBookedService.getTrainBooked(param));
    }


    @ApiOperation("按id查询火车入账信息")
    @GetMapping("/getTrainBookedById")
    Result<TrainBookedEntity> getTrainBookedById(TrainParam param){
        return Result.ok(trainBookedService.getTrainBookedById(param));
    }

    @ApiOperation("修改火车入账信息")
    @PostMapping("/modifyTrainBooked")
    Result<String> modifyTrainBooked(TrainBookedEntity param){
        return Result.ok(trainBookedService.modifyTrainBooked(param));
    }
    /**
     * 批量导入
     */
    /***
     *  导入excel,新增火车入账记录
     **/
    @PostMapping("/importExcel")
    public Result<Map<String,Object>> importExcel(@RequestParam("file") MultipartFile file, String belongsFactory) {
        Map<String,Object> result = new HashMap<>();
        try{
            //解析excel
            List<String[]> strArr = POIUtil.readExcel(file);
            //TODO 保存数据库
            result = trainBookedService.importExcel(file,strArr,belongsFactory);
        }catch (Exception e){
            e.printStackTrace();
            return Result.err("上传异常，请联系管理员！");
        }
        return Result.ok(result);
    }

}
